import datetime
import json
import re
import time
from concurrent.futures import ThreadPoolExecutor

import execjs
import requests

def down_mp4():
    headers = {
        "authority": "playvv.yangshipin.cn",
        "referer": "https://w.yangshipin.cn/",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    url = "https://playvv.yangshipin.cn/playvinfo"
    jsFile = open('央视频.js', 'r', encoding='utf-8').read()
    ckeyG = execjs.compile(jsFile).call('cKey')
    cookies = {
        "guid": ckeyG['guid']
    }
    params = {
        "guid": ckeyG['guid'],
        "platform": "4330701",
        "vid": "g0000648jiz",
        "defn": "auto",
        "charge": "0",
        "defaultfmt": "auto",
        "otype": "json",
        "defnpayver": "1",
        "appVer": "1.26.0",
        "sphttps": "1",
        "sphls": "1",
        "spwm": "4",
        "dtype": "3",
        "defsrc": "1",
        "encryptVer": "8.1",
        "sdtfrom": "4330701",
        "cKey": ckeyG['cKey'],
        "panoramic": "false",
        "flowid": "lok6pczf_najllb5899"
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    print('-----------------视频获取成功！------------------')
    return {'vkey':re.findall('"fvkey":"(.*?)",',response.text,re.S)[0],
            'mp4':re.findall('"fn":"(.*?)",',response.text,re.S)[0],
            'guid':ckeyG['guid']}

def download(MP4):
    headers = {
        "authority": "btrace.yangshipin.cn",
        "content-type": "application/x-www-form-urlencoded",
        "origin": "https://w.yangshipin.cn",
        "referer": "https://w.yangshipin.cn/",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    url = "https://btrace.yangshipin.cn/kvcollect"
    params = {
        "BossId": "2865"
    }
    jsFile = open('央视频.js', 'r', encoding='utf-8').read()
    jsx = execjs.compile(jsFile).call('pids')
    data = {
        "ctime": str(datetime.datetime.now()).split('.')[0],
        "ua": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/119.0.0.0 safari/537.36 edg/119.0.0.0",
        "hh_ua": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/119.0.0.0 safari/537.36 edg/119.0.0.0",
        "platform": "4330701",
        "guid": MP4['guid'],
        "Pwd": '1698957057',
        "version": "wc-1.26.0",
        "url": "https://w.yangshipin.cn/video?type=0&vid=g0000648jiz",
        "hh_ref": "https://w.yangshipin.cn/video?type=0&vid=g0000648jiz",
        "vid": "g0000648jiz",
        "isfocustab": "1",
        "isvisible": "1",
        "idx": "0",
        "val": "1005",
        "pid": jsx['pid'],
        "bi": "0",
        "bt": "0",
        "defn": "hd",
        "vurl": "https://mp4playali-cdn.ysp.cctv.cn/{}?sdtfrom=4330701&guid={}&vkey={}&platform=2".format(MP4['mp4'],MP4['guid'],MP4['vkey']),
        "step": "6",
        "val1": "1",
        "val2": "1",
        "fact1": "",
        "fact2": "",
        "fact3": "",
        "fact4": "",
        "fact5": ""
    }
    print(data)
    response = requests.post(url, headers=headers, params=params, data=data)
    print('刷视频次数：',response.text)
    print('-----------------------------------------------------------')

def downCount():
    headers = {
        "authority": "w.yangshipin.cn",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    cookies = {
        "ysp_uv": "1",
        "guid": "lojyij3k_5vcbdk9uuw"
    }
    url = "https://w.yangshipin.cn/video"
    params = {
        "type": "0",
        "vid": "g0000648jiz"
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    data = re.findall('window.__STATE_video__=(.*?)</script></div> <div id="page-loading" style="display: none;">',response.text,re.S)
    dataDict = json.loads(data[0])
    print('--------------------------视频信息--------------------------')
    author = dataDict['payloads']['sharevideo']['om_info']['title']
    title = dataDict['payloads']['sharevideo']['title']
    subtitle = dataDict['payloads']['sharevideo']['subtitle']
    fans_cnt = dataDict['payloads']['sharevideo']['om_info']['fans_cnt']
    video_cnt = dataDict['payloads']['sharevideo']['om_info']['video_cnt']
    print('作者：',author)
    print('标题：', title)
    print('观看量：', subtitle)
    print('喜爱量：', fans_cnt)
    print('播放次数：', video_cnt)
    print('-----------------------------------------------------------')

def main(start):
    MP4 = down_mp4()
    download(MP4)
    downCount()

def run():
    print('-----------------------开始--------------------------')
    start = time.time()

    pool = ThreadPoolExecutor(5)
    for i in range(1, 100):
        pool.submit(main,start)
    pool.shutdown()

    end = time.time()
    print('执行结束，耗时：',end - start)

if __name__ == '__main__':
    run()

